Skip to content

Fix GPU Hang in MTPTokenIterator: flush Metal graph after verification pass#41

Merged
solderzzc merged 2 commits into
mainfrom
fix/mtp-gpu-hang
May 12, 2026
Merged

Fix GPU Hang in MTPTokenIterator: flush Metal graph after verification pass#41
solderzzc merged 2 commits into
mainfrom
fix/mtp-gpu-hang

Conversation

@solderzzc

Copy link
Copy Markdown
Member

Problem

The MTPTokenIterator at temperature=0 matches TokenIterator output test was consistently crashing CI with:

libc++abi: terminating due to uncaught exception of type std::runtime_error:
[METAL] Command buffer execution failed: Caused GPU Hang Error (00000003:kIOGPUCommandBufferCallbackErrorHang)

Root Cause

MTPTokenIterator.speculateRound() calls model.callMTP() each round but did not immediately evaluate the result. On hybrid SSM/attention models (e.g. Qwen35), the recurrent GatedDeltaNet layers accumulate un-evaluated MLX lazy graph nodes across speculation rounds. After 3-4 rounds the Metal command buffer overflows and triggers the GPU Watchdog.

Fix

Added eval(mtpResult) immediately after the verification callMTP() forward pass, before the acceptance/rejection loop reads .asArray() values. This flushes the full forward-pass graph — including SSM cache state — in one shot each round.

Verification

All 16 MTP speculative decoding tests pass locally including the previously failing test.

…rification pass

On hybrid SSM/attention models (Qwen35), the recurrent GatedDeltaNet layers
accumulate un-evaluated MLX graph nodes across each speculateRound(). Without
an explicit eval() after callMTP(), the Metal command buffer grows across
multiple speculation rounds until it triggers the GPU Watchdog (kIOGPUCommandBufferCallbackErrorHang).

Adding eval(mtpResult) immediately after the verification forward pass flushes
the accumulated graph, preventing the Metal timeout.
Copilot AI review requested due to automatic review settings May 12, 2026 22:28

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR addresses a CI-crashing Metal GPU hang during MTP speculative decoding by forcing evaluation immediately after the verification callMTP() forward pass, preventing lazy-graph growth across speculation rounds (notably on hybrid SSM/attention models like Qwen35).

Changes:

  • Add an eval(mtpResult) right after the verification forward pass in MTPTokenIterator.speculateRound().
  • Document the rationale for flushing/evaluating at that point to avoid GPU watchdog hangs.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread Libraries/MLXLMCommon/Evaluate.swift Outdated
// On hybrid SSM/attention models (e.g. Qwen35), the recurrent SSM layers accumulate
// un-evaluated graph nodes across rounds. Without an explicit sync here the Metal
// command buffer grows until it triggers the GPU Watchdog.
eval(mtpResult)
Comment thread Libraries/MLXLMCommon/Evaluate.swift
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@solderzzc solderzzc merged commit c3467dd into main May 12, 2026
6 checks passed
@solderzzc solderzzc deleted the fix/mtp-gpu-hang branch May 12, 2026 23:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants